package com.btashov.stringarray;

/**
 * @author Bahtiyar Tashov <btashov@gmail.com>
 */

/**
 * todo: Given an image represented by an NxN matrix,
 * where each pixel in the image is 4 bytes,
 * write a method to rotate the image by 90 degrees.
 */
public class MatrixRotation {
    /**
     * Note a few things:
     * <p/>
     * It improves readability to refer to the dimensions of a MxN matrix as M and N
     * It's traditional to use
     * <p/>
     * r, c instead of i, j to index row and column of a matrix
     * <p/>
     * This is not the most robust implementation:
     * Does not ensure that mat is a valid MxN matrix, M>0, N>0
     * Use an explicit mapping formula instead of extraneous local variables
     * Makes program less complex and more readable
     *
     * @param mat 2d array
     * @return clock wised rotated on 90 degrees 2d array
     */
    public static int[][] rotateCW(int[][] mat) {
        final int M = mat.length;
        final int N = mat[0].length;
        int[][] ret = new int[N][M];
        for (int r = 0; r < M; r++) {
            for (int c = 0; c < N; c++) {
                ret[c][M - 1 - r] = mat[r][c];
            }
        }
        return ret;
    }


    public static int[][] rotateNM(int[][] mat) {
        final int M = mat.length;
        final int N = mat[0].length;
        int[][] ret = new int[N][M];
        for (int r = 0; r < M; r++) {
            for (int c = 0; c < N; c++) {
                ret[c][M - 1 - r] = mat[r][c];
            }
        }
        return ret;
    }
}
